package com.fengyi.homefurnish.controller;

import com.fengyi.homefurnish.controller.vo.StoreInfoVo;
import com.fengyi.homefurnish.pojo.DtArea;
import com.fengyi.homefurnish.pojo.SellArea;
import com.fengyi.homefurnish.pojo.StoreInfo;
import com.fengyi.homefurnish.service.StoreInfoService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/depot")
public class StoreInfoController {

    @Resource
    private StoreInfoService storeInfoService;

    @RequestMapping("/depot.html")
    public String searchDepot(Model model){
        model.addAttribute("store",storeInfoService.search());
        return "/depot/depot";
    }

    @RequestMapping("/updateState")
    @ResponseBody
    public String updateState(Integer stateId,Integer storeId){
        String flag=null;
        try{
            storeInfoService.updateStoreState(stateId,storeId);
            flag="true";
        }catch(RuntimeException e){
            flag="false";
        }
        return flag;
    }

    @RequestMapping("/getFirstArea")
    @ResponseBody
    public List<SellArea> getFirstArea(){
        return storeInfoService.getFirstArea();
    }

    @RequestMapping("/getAreaByPid")
    @ResponseBody
    public List<SellArea> getAreaByPid(Integer pId){
        return storeInfoService.getAreaByPid(pId);
    }

    @RequestMapping("/saveStoreInfo")
    @ResponseBody
    public String addStoreInfo(StoreInfo storeInfo){
        String flag = null;
        try{
            storeInfoService.saveStoreInfo(storeInfo);
            flag="true";
        }catch(RuntimeException e){
            flag="false";
        }
        return flag;
    }

    @RequestMapping("/searchInfoById")
    @ResponseBody
    public StoreInfoVo searchStoreInfoById(Integer storeInfoId){
        return storeInfoService.searchStoreInfoById(storeInfoId);
    }

    @RequestMapping("/updateStoreInfo")
    @ResponseBody
    public String updateStoreInfo(StoreInfo storeInfo){
        String flag = null;
        try{
            storeInfoService.updateStoreInfo(storeInfo);
            flag="true";
        }catch(RuntimeException e){
            flag="false";
        }
        return flag;
    }

    @RequestMapping("/batchDelStoreInfo")
    @ResponseBody
    public String delStoreInfo(String[] storeIds){
        System.out.println(storeIds.length);
        String flag = null;
        try{
            storeInfoService.batchDelStoreInfo(storeIds);
            flag="true";
        }catch(RuntimeException e){
            flag="false";
        }
        return flag;
    }

}
